package org.apache.lucene.store;

import java.io.IOException;

import org.apache.lucene.GAERAMIndexProvider;
import org.apache.lucene.IndexProvider;
import org.apache.lucene.index.IndexWriter;
import org.junit.Before;
import org.junit.Test;

import joelwilson.GAETestCase;
import junit.framework.Assert;

public class GAERAMIndexProviderTestCase extends GAETestCase {

	IndexProvider provider;
	
	@Override
	@Before
	public void setUp() {
		super.setUp();
		provider = new GAERAMIndexProvider("test");
	}
	
	@Test
	public void testGetDirectory() throws IOException {
		Assert.assertNotNull("Directory is not null", provider.getDirectory());
	}
	
	@Test
	public void testGetIndexSearcher() throws IOException {
		try {
			provider.getIndexSearcher();
			Assert.fail("IOException should be thrown because there is no index to read from the directory."); 
		} catch (IOException ioException) {}
	}
	
	@Test
	public void testGetIndexWriter() throws IOException {
		IndexWriter writer = provider.getStandardIndexWriter();
		Assert.assertNotNull("IndexWriter is not null", writer);
		
		try {
			provider.getStandardIndexWriter();
			Assert.fail("LockObtainFailedException should have been thrown");
		} catch (LockObtainFailedException lockException) {
		}
		
		writer.close();
		
		Assert.assertNotNull("2nd IndexWriter is not null", writer);
	}
	
	@Test
	public void testWriteThenSearch() throws IOException {
		IndexWriter writer = provider.getStandardIndexWriter();
		writer.close();
		
		Assert.assertNotNull("IndexSearcher is not null", provider.getIndexSearcher());
	}
}
